﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.ComponentModel;
using System.Windows.Forms;
using TeachMe.Application.Properties;

namespace TeachMe.Application.Controls
{
    public class IntervalComboBox : ComboBox
    {
        private class IntervalItem
        {
            public Interval Interval { get; set; }
            public string Caption { private get; set; }

            public override string ToString()
            {
                return Caption;
            }
        }

        public IntervalComboBox()
        {
            DropDownStyle = ComboBoxStyle.DropDownList;
        }

        public void FillIntervals()
        {
            Items.Add(new IntervalItem { Caption = Resources.IntervalNever, Interval = Interval.Never });
            Items.Add(new IntervalItem { Caption = Resources.IntervalDaily, Interval = Interval.Daily });
            Items.Add(new IntervalItem { Caption = Resources.IntervalWeekly, Interval = Interval.Weekly });
        }

        [Browsable(false)]
        [Localizable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Interval Interval
        {
            get
            {
                IntervalItem selection = SelectedItem as IntervalItem;
                if (selection == null)
                {
                    throw new InvalidOperationException();
                }

                return selection.Interval;
            }
            set
            {
                foreach (IntervalItem intervalItem in Items)
                {
                    if (intervalItem.Interval == value)
                    {
                        SelectedItem = intervalItem;
                    }
                }
            }
        }

        public object GetValue()
        {
            return Text;
        }
    }
}
